package org.siqisource.webstart.core.utils.condition;

import java.util.ArrayList;
import java.util.List;

import org.siqisource.webstart.core.utils.converter.NameConverter;

public class Condition {


	/** 查询条件集合 */
	private List<Expression> expressions = new ArrayList<Expression>();

	/** orderBy */
	private String orderBy = null;

	/** groupBy */
	private String groupBy = null;
	

	public Condition() {

	}
	 
	
	public Condition copy(){
		Condition condition = new Condition();
		condition.orderBy = this.orderBy;
		condition.groupBy = this.groupBy;
		condition.expressions = new ArrayList<Expression>();
		for(Expression expression : this.expressions){
			condition.expressions.add(expression);//浅copy
		}
		return condition;
	}

	public String getCondition() {
		StringBuffer sql = new StringBuffer();

		if (expressions.size() > 0) {
			for (int i = 0, iSize = expressions.size(); i < iSize; i++) {
				Expression expression = expressions.get(i);
					sql.append(expression.toStmt(i));
			}
		}

		return sql.toString();
	}
	
	
	
	public String toString(){
		return this.getCondition();
	}

	/**
	 * isNull、isNotNull时调�?
	 * 
	 * @param fieldMapping
	 *            字段与属性映射关系类
	 * @param logicalSymbol
	 *            逻辑操作�?and 或�?or
	 * @param leftParenthesis
	 *            左括�?
	 * @param properityName
	 *            属�?�?
	 * @param compareSymbol
	 *            比较�?
	 * @param rightParenthesis
	 *            右括�?
	 */
	public void noValue(String logicalSymbol, String leftParenthesis, String properityName, String compareSymbol,
			String rightParenthesis) {
		Expression expression = new Expression(logicalSymbol, leftParenthesis, properityName, compareSymbol, null,
				null, rightParenthesis);
		getNotNullConditions().add(expression);
	}

	/**
	 * = != > < 等条件时调用
	 * 
	 * @param fieldMapping
	 *            字段与属性映射关系类
	 * @param logicalSymbol
	 *            逻辑操作�?and 或�?or
	 * @param leftParenthesis
	 *            左括�?
	 * @param properityName
	 *            属�?�?
	 * @param compareSymbol
	 *            比较�?
	 * @param value
	 *            比较�?
	 * @param rightParenthesis
	 *            右括�?
	 */
	public void singleValue(String logicalSymbol, String leftParenthesis, String properityName, String compareSymbol,
			Object value, String rightParenthesis) {
		Expression expression = new Expression(logicalSymbol, leftParenthesis, properityName, compareSymbol, value,
				null, rightParenthesis);
		getNotNullConditions().add(expression);
	}

	/**
	 * between 时�?调用
	 * 
	 * @param fieldMapping
	 *            字段与属性映射关系类
	 * @param logicalSymbol
	 *            逻辑操作�?and 或�?or
	 * @param leftParenthesis
	 *            左括�?
	 * @param properityName
	 *            属�?�?
	 * @param compareSymbol
	 *            比较�?
	 * @param value
	 *            比较�?
	 * @param secondValue
	 *            between的第二个比较�?
	 * @param rightParenthesis
	 *            右括�?
	 */
	public void betweenValue(String logicalSymbol, String leftParenthesis, String properityName, String compareSymbol,
			Object value, Object secondValue, String rightParenthesis) {
		Expression expression = new Expression(logicalSymbol, leftParenthesis, properityName, compareSymbol, value,
				secondValue, rightParenthesis);
		getNotNullConditions().add(expression);
	}

	/**
	 * 使用in条件的时候进行调�?
	 * 
	 * @param logicalSymbol
	 *            逻辑操作�?and 或�?or
	 * @param leftParenthesis
	 *            左括�?
	 * @param properityName
	 *            属�?�?
	 * @param compareSymbol
	 *            比较�?
	 * @param value
	 *            比较�?
	 * @param rightParenthesis
	 *            右括�?
	 */
	public void listValue(String logicalSymbol, String leftParenthesis, String properityName, String compareSymbol,
			Object value, String rightParenthesis) {
		Expression expression = new Expression(logicalSymbol, leftParenthesis, properityName, compareSymbol, value,
				null, rightParenthesis);
		getNotNullConditions().add(expression);
	}

	public List<Expression> getConditions() {
		return expressions;
	}

	/**
	 * 获得非空的conditions
	 * 
	 * @return 非空的conditions
	 */
	public List<Expression> getNotNullConditions() {
		if (expressions == null) {
			expressions = new ArrayList<Expression>();
		}
		return expressions;
	}

	/**
	 * 设置查询条件的orderBy属�?
	 * 
	 * @param propertiesName
	 *            用�?号分隔开的属性名
	 * @param direction
	 *            排序方向
	 * @param fieldMapping
	 *            字段属�?与数据库字段的映射关系对�?
	 */
	public void orderBy(String propertiesName, String direction) {
		StringBuffer sbOrderBy = new StringBuffer(" ORDER BY ");
		String fieldsName = getColumns(propertiesName);
		sbOrderBy.append(fieldsName);
		this.orderBy = sbOrderBy.append(" ").append(direction).toString();
	}

	/**
	 * 设置查询条件的groupBy属�?
	 * 
	 * @param propertiesName
	 *            用�?号分隔开的属性名
	 * @param fieldMapping
	 *            字段属�?与数据库字段的映射关系对�?
	 */
	public void setGroupBy(String propertiesName) {
		StringBuffer sbGroupBy = new StringBuffer(" GROUP BY ");
		String fieldsName = getColumns(propertiesName);
		sbGroupBy.append(fieldsName);
		this.groupBy = sbGroupBy.toString();
	}

	public String getGroupBy() {
		return groupBy;
	}

	public String getOrderBy() {
		return orderBy;
	}

	private String getColumns(String propertiesName) {
		StringBuffer columns = new StringBuffer();
		String property[] = propertiesName.split(" ");
		for (int i = 0; i < property.length; i++) {
			columns.append(" ");
			columns.append(NameConverter.propertyToColumn(property[i].trim()).toLowerCase());
		}
		return columns.toString();
	}

}
